import Config from './config.js'

const http = (options) => {
	return new Promise((resolve, reject) => {
		let header = {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
		}
		header.Authorization = uni.getStorageSync('userData').token
		if (options.showLoading !== false) {
			uni.showLoading({
				mask: true
			})
		}
		uni.request({
			method: options.method,
			url: `${Config.baseUrl}${options.url}`,
			data: options.data,
			header: header,
			success: (res) => {
				if (res.data.status === '0') {
					resolve(res)
					if (options !== false) uni.hideLoading()
				} else {
					resolve(res)
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 3000
					})
				}
			},
			fail: (err) => {
				reject(err.data)
			},
			complete() {
				
			}
		})
	}).catch((res) => {
		uni.showToast({
			title: '服务器出错，请联系开发人员',
			duration: 3000,
			icon: 'none'
		})
	})
}

export default http